﻿using System;
using System.Collections.Generic;

using FineUI;

using EDP.Common;
using EDP.Common.Help;
using EDP.Common.Model;
using EDP.Framework.Form;

using School.Model;
using School.Business;

namespace EDP.WebForm.school.student
{
    public partial class edit : WebFormBase
    {
        private readonly StudentBLL bll_Student = StudentBLL.Create();  //创建StudentBLL类实例,通过Create方法创建可实现自动化日志
        private string m_itemId = string.Empty;
        private string m_orgId = string.Empty;

        protected void Page_Load(object sender, EventArgs e) {
            this.m_itemId = UrlHelp.GetUrlParamter("itemId");
            this.m_orgId = UrlHelp.GetUrlParamter("orgId");
            if (!this.IsPostBack) {
                this.btn_close.OnClientClick = ActiveWindow.GetHideReference();

                this.bindClass();
                this.bindData();
            }
        }

        private void bindClass() {
            var list = this.bll_Student.GetOrganizationList().Content;
            if (list == null && !list.HasValue())
                return;

            List<string> types = new List<string> { "001", "003", "004" }; //学校,年级,班级(组织类型编码)
            var classList = list.FindAll(t => types.Contains(t.TypeNumbers));
            if (classList == null || !classList.HasValue())
                return;

            var schoolList = list.FindAll(t => t.PropertyId == 1 && t.Levels == 2); //校区

            if (schoolList != null && schoolList.HasValue()) {
                classList.AddRange(schoolList);
                classList.Sort(SortDirection.ASC, "FullCode"); //添加校区后重新排序
            }

            List<DropDownListTree> treeList = new List<DropDownListTree>();
            bool isSelect = false;
            int levelStep = classList[0].Levels.Value - 1; //DropDownListTree级别从1开始,此处计算步长
            foreach (var item in classList) {
                isSelect = item.TypeNumbers == "004"; //只能选择班级
                treeList.Add(new DropDownListTree(item.ID, item.Name, item.Levels.Value - levelStep, isSelect));
            }

            this.dll_class.DataSource = treeList;
            this.dll_class.DataTextField = "Name";
            this.dll_class.DataValueField = "ID";
            this.dll_class.DataEnableSelectField = "EnableSelect"; //是否启用选择控制,本示例控制了只能选择班级
            this.dll_class.DataBind();

            if (this.m_orgId.HasValue()) {
                var item = treeList.Find(t => t.ID == this.m_orgId);
                if (item != null && item.EnableSelect)
                    this.dll_class.SelectedValue = this.m_orgId;
            }
        }

        /// <summary>
        /// 绑定数据
        /// </summary>
        private void bindData() {
            if (this.m_itemId.HasValue()) {
                var item = this.bll_Student.GetStudent(this.m_itemId).Content;
                if (item != null && item.HasValue())
                    this.BindValue(item); //绑定数据,根据控件BindName的属性名称绑定对应的业务对象的属性
            }
        }

        protected void btn_save_Click(object sender, EventArgs e) {
            StudentModel item = new StudentModel();
            this.SetValue(item);

            ResultModel<bool> result = new ResultModel<bool>();

            //******为了区分新增和修改所记录的日志信息,特地将保存方法分为了新增和修改******
            if (this.m_itemId.HasValue()) {
                item.ID = this.m_itemId;
                result = this.bll_Student.UpdateStudent(item);
            }
            else
                result = this.bll_Student.AddStudent(item);
            //******-----------------------------------------------------------------******

            if (result.Content)
                PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
            else {
                string title = string.Empty;
                if (this.m_itemId.HasValue())
                    title = "修改学生信息";
                else
                    title = "新增学生信息";
                if (!string.IsNullOrWhiteSpace(result.Message)) {
                    if (result.ErrorLevel == (int)ErrorLevelEnum.SystemError)
                        Alert.Show(result.Message, title, MessageBoxIcon.Error);
                    else
                        Alert.Show(result.Message, title, MessageBoxIcon.Warning);
                }
                else
                    Alert.Show("操作失败", title, MessageBoxIcon.Error);
            }
        }
    }
}